package com.guigu.youxuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.common.utils.GoodsInfoVo;
import com.guigu.youxuan.common.utils.PageUtils;
import com.guigu.youxuan.common.vo.GoodsVo;
import com.guigu.youxuan.mapper.GoodsInfoMapper;
import com.guigu.youxuan.pojo.BackUser;
import com.guigu.youxuan.pojo.GoodsCategory;
import com.guigu.youxuan.pojo.GoodsImg;
import com.guigu.youxuan.pojo.GoodsInfo;
import com.guigu.youxuan.pojo.dto.GoodsDto;
import com.guigu.youxuan.service.IGoodsImgService;
import com.guigu.youxuan.service.IGoodsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@Service
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfo> implements IGoodsInfoService {
    @Autowired
    private GoodsInfoMapper goodsInfoMapper;
    @Autowired
    private IGoodsImgService goodsImgService;

    /**
     * 供应商带条件分页查询
     * @param vo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<GoodsInfo> SupplierListPage(GoodsInfoVo vo, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<GoodsInfo> goodsInfos=goodsInfoMapper.list(vo);
        PageInfo<GoodsInfo> pageInfo=new PageInfo<>(goodsInfos);
        return pageInfo;
    }


    /**
     * 模糊查询商品信息
     * @param keyword
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageUtils<GoodsDto> selGoods(String keyword,int pageNo, int pageSize) {

        PageHelper.startPage(pageNo,pageSize);
        List<GoodsDto> list= this.goodsInfoMapper.selGoods(keyword);
        PageInfo<GoodsDto> pageInfo = new PageInfo<>(list);

        PageUtils<GoodsDto> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }

    /**
     * 查询水果热销
     * @return
     */
    @Override
    public List<GoodsDto> selFruits() {
        List<GoodsDto> GoodsDto=goodsInfoMapper.selFruits();
        return GoodsDto;
    }

    /**
     * 查询酒水热销
     * @return
     */
    @Override
    public List<GoodsDto> selDrinks() {
        List<GoodsDto> GoodsDto=goodsInfoMapper.selDrinks();
        return GoodsDto;
    }

    /**
     * ID查询商品详情页信息
     * @param id
     * @return
     */
    @Override
    public List<GoodsInfo> selGoodsById(int id) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",id);
        List goodsById = this.list(wrapper);
        return goodsById;
    }

    @Override
    public PageUtils<GoodsDto> getGoodsList(GoodsInfo goodsInfo, int currentPage, int pageSize) {


        PageHelper.startPage(currentPage,pageSize);
        PageHelper.orderBy("id desc");

        List<GoodsDto> list=this.goodsInfoMapper.getGoodsDto(goodsInfo);
        PageInfo<GoodsDto> pageInfo = new PageInfo<>(list);

        PageUtils<GoodsDto> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }

    @Transactional
    @Override
    public void addGoods(GoodsVo vo) {
        List<Integer> type = vo.getType();
        String goodsCategoryId = type.stream()
                .map(Object::toString)
                .collect(Collectors.joining("-"));
        GoodsInfo goodsInfo = new GoodsInfo();
        goodsInfo.setName(vo.getName());
        goodsInfo.setGoodsCategoryId(goodsCategoryId);
        goodsInfo.setDetail(vo.getDetail());
        goodsInfo.setUnit(vo.getUnit());
        goodsInfo.setPrice(vo.getPrice());
        this.save(goodsInfo);

        GoodsInfo max = goodsInfoMapper.getMaxId();
        GoodsImg goodsImg = new GoodsImg();
        goodsImg.setType(1);
        goodsImg.setGoodsId(max.getId());
        goodsImg.setImgLink(vo.getZanShiTu());
        goodsImgService.save(goodsImg);
    }

    @Override
    public GoodsInfo queryGoodsById(Long id) {
        GoodsInfo byId = this.getById(id);
        return byId;
    }

    @Transactional
    @Override
    public void updateGoods(GoodsVo vo) {
        List<Integer> type = vo.getType();
        String goodsCategoryId = type.stream()
                .map(Object::toString)
                .collect(Collectors.joining("-"));
        GoodsInfo goodsInfo = new GoodsInfo();
        goodsInfo.setId(vo.getId());
        goodsInfo.setName(vo.getName());
        goodsInfo.setGoodsCategoryId(goodsCategoryId);
        goodsInfo.setDetail(vo.getDetail());
        goodsInfo.setUnit(vo.getUnit());
        goodsInfo.setPrice(vo.getPrice());
        this.updateById(goodsInfo);

        if(StringUtils.isEmpty(vo.getImgId()) && !StringUtils.isEmpty(vo.getZanShiTu())) {
            GoodsImg goodsImg = new GoodsImg();
            goodsImg.setImgLink(vo.getZanShiTu());
            goodsImg.setType(1);
            goodsImg.setGoodsId(vo.getId());
            goodsImgService.save(goodsImg);
        }

        if(!StringUtils.isEmpty(vo.getImgId()) && !StringUtils.isEmpty(vo.getZanShiTu())) {
            QueryWrapper<GoodsImg> wrapper = new QueryWrapper<>();
            wrapper.eq("id",vo.getImgId());
            GoodsImg one = goodsImgService.getOne(wrapper);
            one.setImgLink(vo.getZanShiTu());
            goodsImgService.updateById(one);
        }
    }

    @Override
    public void delGoods(GoodsInfo goodsInfo) {
        if(goodsInfo != null) {
            goodsInfo.setIsDeleted((byte) 1);
            this.updateById(goodsInfo);
        }
    }

    @Override
    public void delsGoods(List<GoodsInfo> goodsInfoList) {
        for(GoodsInfo goodsInfo : goodsInfoList) {
            goodsInfo.setIsDeleted((byte)1);
            this.updateById(goodsInfo);
        }
    }

    @Override
    public void addStockUp(Integer amount, Long goodsId) {
        GoodsInfo byId = this.getById(goodsId);
        byId.setStockUp(byId.getStockUp()+amount);
        this.updateById(byId);
    }

    @Override
    public void updateStatus(GoodsInfo goodsInfo) {
        this.updateById(goodsInfo);
    }
}
